package com.wchen.project.zk.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.utils.EnsurePath;

/**
 * @Desc:   Curator 工具类 EnsurePath 使用示例
 *          使用场景：上层业务希望对一个数据节点进行一些操作，但是操作之前需要确保该节点存在
 *          EnsurePaths 类以过时，替代功能为
 *          Prefer CuratorFramework.create().creatingParentContainersIfNeeded() 或者
 *          CuratorFramework.exists().creatingParentContainersIfNeeded()
 * @author: cww
 * @DateTime: 2020-01-25 16:00
 */

public class EnsurePathDemo {
    static String path = "/zk-book/c1";
    static CuratorFramework client = CuratorFrameworkFactory.builder()
            .connectString("home:2181")
            .sessionTimeoutMs(5000)
            .retryPolicy(new ExponentialBackoffRetry(1000, 3))
            .build();

    public static void main(String[] args) throws Exception {
        client.start();
        client.usingNamespace("zk-book");

        EnsurePath ensurePath = new EnsurePath(path);
        ensurePath.ensure(client.getZookeeperClient());
        ensurePath.ensure(client.getZookeeperClient());

        EnsurePath ensurePath1 = client.newNamespaceAwareEnsurePath("/c1");
        ensurePath1.ensure(client.getZookeeperClient());
    }
}
